package cn.com.miaoto.business.controller;

import cn.com.miaoto.base.common.BaseController;
import cn.com.miaoto.base.common.PageResult;
import cn.com.miaoto.base.common.PageSearch;
import cn.com.miaoto.base.common.ReturnMsg;
import cn.com.miaoto.business.model.Visit;
import cn.com.miaoto.business.service.VisitService;
import cn.com.miaoto.business.vo.VisitInfo;
import cn.com.miaoto.model.LoginUser;
import com.github.pagehelper.PageHelper;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 客户拜访记录
 * Created by guo.chen on 2018/9/17.
 */
@RestController
@RequestMapping("visit")
public class VisitController extends BaseController {

    @Resource
    private VisitService visitService;

    @GetMapping("selectPageList")
    public PageResult selectPageList(HttpServletRequest request, PageSearch pageSearch){
        Map params = getParameterMap(request);
        try{
            PageHelper.startPage(pageSearch.getPage(), pageSearch.getLimit());
            List<VisitInfo> visitInfoList = this.visitService.selectVisitList(params);
            return new PageResult<VisitInfo>(visitInfoList);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("查询拜访记录列表失败");
            return new PageResult();
        }
    }

    @GetMapping("selectByPrimaryKey")
    public ReturnMsg selectByPrimaryKey(Integer id){
        try{
            Visit visit = this.visitService.selectByKey(id);
            return new ReturnMsg(true,visit);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("根据主键查询拜访信息失败");
            return new ReturnMsg(false);
        }
    }

    @PostMapping("edit")
    public ReturnMsg insertOrUpdate(HttpSession session, @RequestBody Visit visit){
        LoginUser loginUser =(LoginUser) session.getAttribute("currentUser");
        try {
            if(StringUtils.isEmpty(visit.getId())){
                visit.setCreator(loginUser.getUserId());
                visit.setCreateTime(LocalDateTime.now());
                return new ReturnMsg(this.visitService.saveNotNull(visit)>0,"新增客户拜访信息成功");
            } else{
                visit.setCreator(loginUser.getUserId());
                return new ReturnMsg(this.visitService.updateNotNull(visit)>0,"修改客户拜访信息成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("编辑客户拜访信息失败");
            return new ReturnMsg(false);
        }
    }

    @PostMapping("insert")
    public ReturnMsg insert(Visit visit){
        try {
            return new ReturnMsg(this.visitService.saveNotNull(visit)>0);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("新增拜访信息失败");
            return new ReturnMsg(false);
        }
    }

    @PutMapping("update")
    public ReturnMsg update(Visit visit){
        try {
            return new ReturnMsg(this.visitService.updateNotNull(visit)>0);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("修改拜访信息失败");
            return new ReturnMsg(false);
        }
    }

    @DeleteMapping("delete")
    public ReturnMsg delete(Visit visit){
        try {
            return new ReturnMsg(this.visitService.updateNotNull(visit)>0);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("删除拜访信息失败");
            return new ReturnMsg(false);
        }
    }

    @DeleteMapping("deleteByIds")
    public ReturnMsg deleteByIds(String ids){
        try {
            return new ReturnMsg(this.visitService.deleteByIds(ids)>0);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("删除客户信息失败");
            return new ReturnMsg(false);
        }
    }
}
